package ch07;

import java.util.HashMap;
import java.util.Map;

public class HashMapMain {
    private static void test1(){
        HashMap<String, String > hashMap = new HashMap<>();
        hashMap.put("京","北京市");
        hashMap.put("粤","广东省");
        hashMap.put("湘","湖南省");
        hashMap.put("沪","上海市");
        hashMap.put("首都","北京市");// key重复 value覆盖
        System.out.println(hashMap.size());
        System.out.println(hashMap);
        System.out.println(hashMap.keySet());
        for (String key: hashMap.keySet()) {
            System.out.println(key+" : "+hashMap.get(key));  // k->v
        }
        System.out.println(hashMap.values());
        for (String value : hashMap.values()){
            System.out.println(value);
        }
    }
    private static void test2(){
        Map<Integer,Student> map = new HashMap<>();
        Student s1 = new Student(1,"刘备",28);
        Student s2 = new Student(2,"关羽",27);
        Student s3 = new Student(3,"张飞",25);
        Student s4 = s2;
        Student s5 = new Student(5,"张飞",25);
        map.put(s1.getId(),s1);
        map.put(s2.getId(),s2);
        map.put(s3.getId(),s3);
        map.put(s4.getId(),s4);
        map.put(s5.getId(),s5);
        System.out.println(map.size());
        System.out.println(map);
        for (Integer id : map.keySet()){
            System.out.println(id +" : "+map.get(id)); // k的循环 然后获取v
        }
        for (Map.Entry<Integer, Student> entry : map.entrySet()){
            System.out.println(entry.getKey()+" -> "+entry.getValue());// k-v映射的循环
        }
    }
    public static void main(String[] args) {
//        test1();
        test2();
    }

}
